from flask import Flask, render_template, url_for
import os
import datetime

app = Flask(__name__)


@app.route('/')
def hello_world():
    # 获取当前路径
    root_path = os.path.split(os.path.realpath(__file__))[0]
    file_path = os.path.join(root_path, 'static')
    files = os.listdir(file_path)
    file_list = []
    for f in files:
        if f.startswith('.'):
            # 忽略隐藏文件
            continue
        file = os.path.join(file_path, f)
        if not os.path.isfile(file):
            # TODO 尝试使用get方式传递
            # 暂时pass
            continue
        # 读取文件信息
        stat_info = os.stat(file)
        file_info = {'file_uri': url_for('static', filename=f), 'filename': f,
                     'modify_time': datetime.datetime.fromtimestamp(os.path.getmtime(file)),
                     'file_size': stat_info.st_size}
        file_list.append(file_info)
    return render_template('index.html', file_list=file_list)


if __name__ == '__main__':
    app.run(host='0.0.0.0')
